package com.example.index.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.index.mapper.ConcernMapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yc.biz.BizException;
import com.yc.pojo.Concern;
import com.yc.pojo.ConcernDTO;
import com.yc.pojo.User;
import com.yc.util.Utils;
import com.yc.vo.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("concern")
public class ConcernController {

    @Resource
    ConcernMapper concernMapper;

    @RequestMapping("insertConcern")
    public Result insertConcern(Integer userId, Integer masterId){
        try {
            Utils.checkEmpty(userId, "请登录后关注!");
            Utils.checkEmpty(masterId,"系统繁忙，请稍后重试!");
            Concern concern = new Concern();
            concern.setUserId(userId);
            concern.setMasterId(masterId);
            concernMapper.insert(concern);
            return Result.success("关注成功!");
        } catch (BizException e) {
            e.printStackTrace();
            return Result.failure(e.getMessage());
        }
    }

    @RequestMapping("deleteConcern")
    public Result deleteConcern(Integer userId, Integer masterId){
        try {
            Utils.checkEmpty(userId, "系统繁忙，请稍后重试!");
            Utils.checkEmpty(masterId,"系统繁忙，请稍后重试!");
            LambdaQueryWrapper<Concern> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Concern::getUserId, userId).eq(Concern::getMasterId,masterId);
            concernMapper.delete(lqw);
            return Result.success("取消成功!");
        } catch (BizException e) {
            e.printStackTrace();
            return Result.failure(e.getMessage());
        }
    }

    @RequestMapping("selectConcern")
    public Map<String,Object> selectConcern(Integer userId, Integer masterId){
        boolean isConcern = false;
        boolean isIdentical = false;
        LambdaQueryWrapper<Concern> clqw = null;
        Concern concern = null;
        if (userId != null){
            clqw = new LambdaQueryWrapper<>();
            clqw.eq(Concern::getMasterId,masterId)
                    .eq(Concern::getUserId,userId);
            if(clqw != null){
                concern = concernMapper.selectOne(clqw);
            }
        }
        if (concern != null){
            isConcern = true;
        }
        if (userId == masterId){
            isIdentical = true;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("isConcern",isConcern);
        map.put("isIdentical",isIdentical);
        return map;
    }

    @RequestMapping("selectConcerns")
    public List selectConcerns(Integer userId){
        MPJLambdaWrapper mpjLambdaWrapper = new MPJLambdaWrapper<ConcernDTO>()
                .selectAll(User.class)
                .selectAll(Concern.class)
                .innerJoin(User.class, User::getId, Concern::getMasterId)
                .eq(Concern::getUserId, userId);
        final List<ConcernDTO> list = concernMapper.selectJoinPage(new Page<>(1, 5), ConcernDTO.class, mpjLambdaWrapper).getRecords();
        return list;
    }
}
